<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
require 'define_id.php';

function subtext($text, $length)
{

    if (mb_strlen($text, 'utf8') > $length)

        return mb_substr($text, 0, $length, 'utf8') . ' ...';

    return $text;

}

function getImg($data)
{
    $img_arr = ['image', 'cover_image'];
    foreach ($data['data'] as $key => $value) {//添加图片地址
        foreach ($img_arr as $img_item)
            if (isset($data['included'])) {
                for ($j = 0; $j < count($data['included']); $j++) {
                    if ($data['included'][$j]['id'] == $data['data'][$key]['relationships'][$img_item]['data']['id']) {
                        if (isset($data['included'][$j]['meta']['derivatives'])) {
                            if (isset($data['data'][$j]['relationships']['category']) && $data['data'][$j]['relationships']['category']['data']['id'] == config('slides.banner')) {//bannerq
                                $data['data'][$key]['attributes'][$img_item . 'url'] = $data['included'][$j]['meta']['derivatives']['1600w'];
                            } else {
                                $data['data'][$key]['attributes'][$img_item . 'url'] = $data['included'][$j]['meta']['derivatives']['large'];
                            }
                        }
                        break;
                    }
                }
            } else {
                $data['data'][$key]['attributes'][$img_item . 'url'] = '';
            }
    }
    return $data;
}

/*
 * @param   object  $column_des 目标栏目data
 * @param   array   $columns    所有栏目信息data
 * @return  int                 该栏目是第几栏目
 */
function get_level($column_des, &$columns, $level = 1)
{
    if (empty($column_des['relationships']['parent']['data'][0])) return $level;
    $p_id = $column_des['relationships']['parent']['data'][0]['id'];
    foreach ($columns as $column) {
        if ($column['id'] == $p_id) {
            $level++;
            return get_level($column, $columns, $level);
        }
    }
}

/*
 * @param  string $p_id    父级栏目id
 * @param  Array  $columns 所有栏目信息
 * @return array    返回栏目的所有子栏目object
 */
function get_child_columns(&$p_id, &$columns)
{
    if (empty($p_id)) return null;
    $result = [];
    foreach ($columns as $column) {
        if ($column['relationships']['parent']['data'][0]['id'] == $p_id) {
            array_push($result, $column);
        }
    }
    return $result;
}

/*
 * @param  array $_data 需要判断额外内容的数据
 */

function card_info_special_page_extra(&$_data)
{
    $_data = getImg($_data);
    //$_data_img = null;
    /*if (isset($data['data']['include'])) {
        for ($i = 0; $i < count($_data['data']); $i++) {
            if ($data['data'][$i]['include']['id'] = $data['data'][0]['id']) {
                $_data_img = $data['data'][0]['attributes']['imageurl'];
            }
        }
    }*/
    foreach ($_data['data'] as $kk => $vv) { //判断是否外链文章
        if ($_data['data'][$kk]['attributes']['external_url'] != null) {
            $_data['data'][$kk]['external_url'] = '_blank';
        } else {
            $_data['data'][$kk]['external_url'] = '';
        }
    }
}

/*
 * 整理卡片的信息
 * @param   Api     $A      API接口对象
 * @param   array   $cates  需要整理的栏目对象
 * @param   array   $data   结果存储对象
 * @return  int     栏目个数
 */
function card_info_special_page(&$A, &$cates, &$data)
{
    define('PAGE_MAX', 5);
    $get_column = $A->get_menu('columns'); //获取栏目信息
    $i = 0;
    for ($i = 0; $i < count($cates); $i++) {
        $value = array_values($cates)[$i];
        $key = "data" . ($i + 1);
        if (in_array($value['id'], COLUMNS_HIDE)) continue;
        $dar3 = get_child_columns($value['id'], $get_column['data']);
        //有三级栏目
        if (!empty($dar3)) {
            $_data = $A->get_sub_articlelist(1, $dar3[0]['id'], PAGE_MAX, 1);
            $_data['included'] = [];
            foreach ($dar3 as $j => $child_col) {
                if ($j == 0) continue;
                $_data_tmp = $A->get_sub_articlelist(1, $child_col['id'], PAGE_MAX, 1);
                card_info_special_page_extra($_data_tmp);
                foreach ($_data_tmp['data'] as $_data_tmp_item_data) {
                    //if (count($_data['data'])>=$value['page_limit']) break;
                    array_push($_data['data'], $_data_tmp_item_data);
                }
                //if (count($_data['data'])>=$value['page_limit']) break;
            }
            uasort($_data['data'], "sort_by_date");
            $_data['data'] = array_slice($_data['data'], 0, PAGE_MAX);
        } else {
            $_data = $A->get_sub_articlelist(1, $value['id'], $value['page_limit'], 1);
            card_info_special_page_extra($_data);
        }
        //首页cates有自带key
        $data[array_key_exists('xwzx', $cates) ? array_keys($cates)[$i] : $key] = [
            'id' => $value['id'],
            'list' => $_data['data'],
            //'img' => $_data_img
        ];
    }
    return $i;
}

/*
 * @param array $a
 * @param array $b
 * return       boolean
 */
function sort_by_date($a, $b)
{
    if ($a['attributes']['sticky'] && !$b['attributes']['sticky']) return false;
    else if ($b['attributes']['sticky']) return true;
    else return strcmp($a['attributes']['created'], $b['attributes']['created']) < 0;
}

/*
 * @param   Api     $A           API接口对象
 * @param   array   $get_column  所有的栏目对象数组
 * @param   int     $id          哪个栏目需要寻找一级栏目
 * @return  int          返回一级栏目id
 */

function find_the_dar1(&$A, &$get_column, &$id)
{
    $item = [];
    foreach ($get_column['data'] as $col) {
        if ($col['id'] == $id) $item = $col;
    }
    $p = $item['relationships']['parent']['data'];
    if (empty($p)) return $item['id'];
    else $p_id = $p[0]['id'];
    foreach ($get_column['data'] as $col) {
        if ($col['id'] == $p_id) return find_the_dar1($A, $get_column, $p_id);
    }
}


error_reporting(E_ERROR | E_WARNING | E_PARSE);
